#coding:utf8
'''
读取一个Frame的数据，对数据进行排序，打印数据
'''
import os
import sys
import re
from matplotlib import pyplot as plt



class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


fps = re.compile('Protocol:(.*):(.*)')
FC = re.compile('FC:(.*):(.*):(.*)')
BEGIN = re.compile("BEGIN:")
END = re.compile("END:")


fn = sys.argv[1]

lines = open(fn).readlines()
fpss = []

state = 0
FunCalls = {}
def cmp1(a, b):
    c1 = a[1][0]
    c2 = b[1][0]
    if c1 > c2:
        return -1
    elif c1 < c2:
        return 1
    else:
        return 0

count = 0
def sortFrame():
    ls = []
    global FunCalls
    global count
    for k, v in FunCalls.iteritems():
        temp = [k, v]
        ls.append(temp)


    ls.sort(cmp=cmp1)
    print "Frame:", count
    count += 1
    for l in ls:
        print l
    print "FinishFrame:"

    FunCalls = {}

for l in lines:
    if state == 0:
        ret = BEGIN.findall(l)
        if len(ret) > 0:
            state = 1
        else:
            pass
    elif state == 1:
        ret = FC.findall(l)
        if len(ret) > 0:
            FunCalls[ret[0][0]] = [float(ret[0][1]), int(ret[0][2])]
        else:
            ret = END.findall(l)
            if len(ret) > 0:
                sortFrame()
                state = 0

    





plt.show()
